<?php
include('action.abs.php');
class groupon_mod extends action {
	function groupon_mod() {
		parent :: action();
	}

   /*
    * 添加
    */
	function add() {
		$id = intval(V('g:id'));
		$rs = DR('grouponCom.getGrouponById', '', $id);	//获取数据
		if ($this->_isPost()) {
			$id = intval(V('p:id'));
			$name = trim(V('p:name'));
			$type = trim(V('p:type'));
			$unit = trim(V('p:unit'));
			$brand = trim(V('p:brand'));
			$price = trim(V('p:price'));
			//$state = trim(V('p:state'));
			$remark = trim(V('p:remark'));
			$start = trim(V('p:start'));
			$end = trim(V('p:end'));
			$freight = trim(V('p:freight'));
			$taxrate = trim(V('p:taxrate'));			
			$maxpersons = trim(V('p:maxpersons'));
			$minpersons = trim(V('p:minpersons'));
			//$currentpersons = trim(V('p:currentpersons'));
			//$currentdiscount = trim(V('p:currentdiscount'));

			if (empty($name)||empty($price)) {
				$this->_error('类型和价格不能为空', array('add'));
			}
									
			$data = array(
							'name' => $name,
							'price' => $price,
							'type' => $type,
							'unit' => $unit,
							'brand' => $brand,
							'start' => $start,
							'end' => $end,
							'freight' => $freight,
							'taxrate' => $taxrate,
							'maxpersons' => $maxpersons,
							'minpersons' => $minpersons,
							//'currentpersons' => $currentpersons,
							//'currentdiscount' => $currentdiscount,
							'remark' => $remark
						);
			$rs = DR('grouponCom.saveGrouponById', '', $data,$id);
			if ($rs['rst']) {
				$this->_succ('操作已成功', array('search'));
			}
			$this->_error('添加失败', array('add'));
		}
		TPL :: assign('info', $rs['rst']);
		TPL :: display('mgr/groupon_add', '', 0, false);

	}

	/*
    * 搜索
    */
	function search() {
		$name = urldecode(V('r:keyword', ''));
		$page = (int)V('g:page', 1);
		$each = (int)V('g:each', 15);
		$offset = ($page -1) * $each;
		$num = ($page -1) * $each;

		$rss = $rs = "";
		$rss = DR('grouponCom.getGrouponByName', '', $name, $offset, $each);

        foreach($rss['rst'] as $value) {
            $rs[$value['id']] = $value;
        }
        $count = count($rss['rst']);

		$pager = APP :: N('pager');
		$page_param = array('currentPage'=> $page, 'pageSize' => $each, 'recordCount' => $count, 'linkNumber' => 10);
		$pager->setParam($page_param);

		TPL :: assign('num', $num);
		TPL :: assign('pager', $pager->makePageForKeyWord('',array('keyword'=>urlencode($name))));
		TPL :: assign('count', $count);
		TPL :: assign('name', $name);
        TPL :: assign('list', $rs); 
        TPL :: display('mgr/groupon_list', '', 0, false);
	}


	/*
     * 删除
     */
	function del() {
		$id = V('g:id', 0);
		if (!$id) {
			$this->_error('id不能为空', array('search'));
		}
		
		$p = DR('adminCom.getAdminById', '', $this->_getUid());	//获取当前操作者的数据
		if(!$p['rst']['isroot']) {
			$this->_error('您无权限删除', array('search'));
		}

		$rs = DR('grouponCom.delGroupon', '', $id);
		if ($rs['rst']) {
			$this->_succ('操作已成功', array('search'));
		}
		
		$this->_error('删除失败',  array('search'));
	}
}
